<?php

include_once "function_return_value.php";
#define ("XMLFILE" ,"/usr/local/etc/config/config.xml");
define ("XMLFILE" ,"config.xml");

function OpenXmlFileForWrite($XmlFile)
{
	$Ret_Array = array();

	if ( !file_exists($XmlFile)) 
	{
		return NULL;
	}
	
	$Ret_Array["doc"] = new DOMDocument();
	$Ret_Array["doc"]->formatOutput = true;
	$Ret_Array["doc"]->preserveWhiteSpace = false;

	$Ret_Array["doc"]->load($XmlFile);
	# For DTD check.
	//if (!$Ret_Array["doc"]->validate()) 
	//{
	//   echo "This document is invalid!\n";
	//}

	$Ret_Array["xpath"] = new Domxpath($Ret_Array["doc"]);
	
	return $Ret_Array;
}

function OpenXmlFileForRead($XmlFile)
{
	$Ret_Array = OpenXmlFileForWrite($XmlFile);
	$xpath = $Ret_Array["xpath"];

	return $xpath;
}


#$query_string = "/MINI/NETWORK/DNSS/DNS";
function CheckNodeExist($query_string)
{
	$xpath = OpenXmlFileForRead(XMLFILE);
	
	$list = explode ("/",$query_string);
	
	$path = "/";
	$roots = $xpath->query($patch);
	foreach ($roots as $root)
	{
		$ret = $root->hasChildNodes();
		if ($ret)
		{
			$childs = $ret->childNodes;
			foreach ( $childs as $child)
			{
				;
			}
		}
	}
	$root = $roots->item(0);

	foreach ($roots as $r)
	{
		print $r->nodeName;
	}
	print count($roots);

	/*
	foreach ($list as $l)
	{
		$patch .= $l. "/";
		$results = $xpath->query($patch);
		foreach ($results as $s)
		{
			$iflag = $s->hasChildNodes();

		}

	}
	print $patch;
	*/

}

#$query_string = "/MINI/NETWORK/DNSS/DNS";
function GetAllNodeValue($query_string)
{
	$xpath = OpenXmlFileForRead(XMLFILE);
	$results = $xpath->query($query_string);

	return $results;
}
#$results = GetAllNodeValue($query_string);
#foreach ($results as $result)
#{
#	print $result->nodeValue;
#}

#$query_string = "/MINI/NETWORK/DNSS/DNS";
function GetNumberOfNode($query_string)
{
	$xpath = OpenXmlFileForRead(XMLFILE);
	$evaluate_string = 'count('.$query_string.')';
	$entries = $xpath->evaluate($evaluate_string);

	return $entries;
}
#print "Number:". GetNumberOfNode($query_string);

#$query_string = "/MINI/SYSSTATUS/COMPANY";
#$query_string = "/MINI/NETWORK/ROUTES/STATIC_ROUTE[@dev='eth1']";
function GetAttribute($query_string)
{
	$xpath = OpenXmlFileForRead(XMLFILE);
	$results = $xpath->query($query_string);
	return $results;
}
#$results = GetAttribute($query_string);
#foreach ($results as $result)
#{
#	print $result->getAttribute('country');
#	print $result->getAttribute('DstIp');
#	print $result->nodeValue;
#}

#$query_string= "/MINI/NETWORK/DNSS/DNS";
function EditNodeValue($query_string , $old_value, $set_value)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
	$doc = $Ret_Array["doc"];
	$xpath = $Ret_Array["xpath"];

	$results = $xpath->evaluate($query_string);
	
	foreach( $results as $result)
	{
		if ($result->nodeValue == $old_value)
		{
			$result->nodeValue = $set_value;
		}
	}
	$doc->save(XMLFILE);
}
#SetNodeValue($query_string, "test", "test2");

#$query_string= "/MINI/NETWORK/DNSS/DNS[3]";
function InsertBeforeNode($query_string, $node_name, $node_value)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
	$doc = $Ret_Array["doc"];
	$xpath = $Ret_Array["xpath"];

	$results = $xpath->query($query_string);
	$result = $results->item(0);
	
	$new_node = $doc->createElement($node_name);
	$new_node->nodeValue = $node_value;
	
	$result->parentNode->insertBefore($new_node,$result);

	$doc->save(XMLFILE);
}
#InsertBeforeNode($query_string, "DNS", "172.131.118.100");

#$query_string= "/MINI/NETWORK/DNSS[last()]";
function AppendNode($query_string, $node_name, $node_value)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
	$doc = $Ret_Array["doc"];
	$xpath = $Ret_Array["xpath"];

	$results = $xpath->evaluate($query_string);
	$result = $results->item(0);
	
	$new_node = $doc->createElement($node_name);
	$new_node->nodeValue = $node_value;
	
	$result->appendChild($new_node);

	$doc->save(XMLFILE);
}
#AppendNode($query_string, "DNS", "172.31.118.1");

#$query_string= "/MINI/NETWORK/DNSS/DNS";
function DeleteNode($query_string, $node_value)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
	$doc = $Ret_Array["doc"];
	$xpath = $Ret_Array["xpath"];

	$parent = $xpath->query($query_string);
	
	if ($node_value != NULL)
		foreach($parent as $node)
		{
			if ($node->nodeValue == $node_value)
			{
				$node->parentNode->removeChild($node);
			}
		}
	else
	{
		foreach($parent as $node)
		{
			$node->parentNode->removeChild($node);
		}
	}

	$doc->save(XMLFILE);
}
#DeleteNode($query_string, NULL);

#$query_string = "/MINI/NETWORK/ROUTES/STATIC_ROUTE[normalize-space(@DstIp)='192.168.10.10']";
#$query_string = "/MINI/NETWORK/ROUTES/STATIC_ROUTE[1]";
function EditNodeAttr($query_string, $attr_string, $set_value)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
	$doc = $Ret_Array["doc"];
	$xpath = $Ret_Array["xpath"];

	$results = $xpath->query($query_string);
	
	foreach ($results as $result)
	{
		$result->setAttribute($attr_string, $set_value);
	}
	$doc->save(XMLFILE);
}
#EditNodeAttr($query_string, "DstIp", "172.31.11.101");

#$query_string = "/MINI/NETWORK/ROUTES/STATIC_ROUTE[1]";
function DeleteNodeAttr($query_string, $attr_string)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
	$doc = $Ret_Array["doc"];
	$xpath = $Ret_Array["xpath"];

	$results = $xpath->query($query_string);
	
	foreach ($results as $result)
	{
		$result->removeAttribute($attr_string);
	}
	$doc->save(XMLFILE);

}
#DeleteNodeAttr($query_string, "DstIp");


#$query_string= "/MINI/NETWORK/ROUTES/STATIC_ROUTE[2]";
function InsertBeforeNodeAttr($query_string, $node_name, $attr_name, $attr_value)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
	$doc = $Ret_Array["doc"];
	$xpath = $Ret_Array["xpath"];

	$results = $xpath->query($query_string);
	$result = $results->item(0);

	$new_node = $doc->createElement($node_name);
	$new_node->setAttribute($attr_name, $attr_value);
	
	$result->parentNode->insertBefore($new_node,$result);

	$doc->save(XMLFILE);
}
#InsertBeforeNodeAttr($query_string, "STATIC_ROUTE","GateWay", "172.131.118.1");

#$query_string= "/MINI/NETWORK/ROUTES";
function AppendNodeAttr($query_string, $node_name, $attr_name, $attr_value)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
	$doc = $Ret_Array["doc"];
	$xpath = $Ret_Array["xpath"];

	$results = $xpath->evaluate($query_string);
	$result = $results->item(0);
	
	$new_node = $doc->createElement($node_name);
	$new_node->setAttribute($attr_name, $attr_value);
	
	$result->appendChild($new_node);
	
	$doc->save(XMLFILE);
}
#AppendNodeAttr($query_string, "STATIC_ROUTE","GateWay", "172.31.118.1");

?>

